import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
import { ArticleEntity } from 'src/api/article/entities/article.entity';
import { SharedEntity } from 'src/api/shared/entities/shared.entity';
import { UserEntity } from 'src/api/user/entities/user.entity';
import { CommentLikeEntity } from './comment.like.entity';

@Entity('comment')
export class CommentEntity extends SharedEntity {
  @Column({
    type: 'varchar',
    name: 'content',
    comment: '内容',
  })
  content: string;

  @Column({
    type: 'enum',
    name: 'status',
    nullable: true,
    enum: ['Verifying', 'VerifySuccess', 'VerifyFail'],
    default: 'Verifying',
    enumName: 'statusEnum',
    comment: '评论状态',
  })
  // 审核中  审核通过   审核未通过
  status: 'Verifying' | 'VerifySuccess' | 'VerifyFail' | null;

  // 对应的文章id
  @Column({
    type: 'varchar',
    length: 36,
    name: 'article_id',
    comment: '对应的文章id',
  })
  articleId: string;

  @ManyToOne(() => ArticleEntity, (article) => article.articleComments, {
    onDelete: 'RESTRICT',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'article_id', referencedColumnName: 'id' }])
  article: ArticleEntity;

  // 对应的根id
  @Column('varchar', { name: 'root_id', nullable: true, length: 36, comment: '一级评论的id' })
  rootId: string | null;

  @ManyToOne(() => CommentEntity, (articleComment) => articleComment.articleComments2, {
    onDelete: 'RESTRICT',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'root_id', referencedColumnName: 'id' }])
  root: CommentEntity;

  // 对应的父级id
  @Column('varchar', { name: 'parent_id', nullable: true, length: 36, comment: '对应的父级id' })
  parentId: string | null;

  @ManyToOne(() => CommentEntity, (articleComment) => articleComment.articleComments, {
    onDelete: 'RESTRICT',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'parent_id', referencedColumnName: 'id' }])
  parent: CommentEntity;

  // 评论的用户
  @Column('varchar', { name: 'user_id', length: 36 })
  userId: string;

  @ManyToOne(() => UserEntity, (user) => user.articleComments, {
    onDelete: 'RESTRICT',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'user_id', referencedColumnName: 'id' }])
  user: UserEntity;

  @OneToMany(() => CommentEntity, (articleComment) => articleComment.parent)
  articleComments: CommentEntity[];

  @OneToMany(() => CommentEntity, (articleComment) => articleComment.root)
  articleComments2: CommentEntity[];

  @OneToMany(() => CommentLikeEntity, (articleCommentLike) => articleCommentLike.comment, {
    cascade: true,
  })
  likes: CommentLikeEntity[];
}
